package com.zmn.oms.api.controller.third.tmall;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.utils.RequestUtil;
import com.zmn.oms.third.tmall.TmallConsts;
import com.zmn.oms.third.tmall.dto.TmallOrderCreateData;
import com.zmn.oms.third.tmall.dto.TmallOrderDataBase;
import com.zmn.oms.third.tmall.dto.TmallOrderReserveData;
import com.zmn.oms.third.tmall.service.TmallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 天猫勤鸽 controller
 *
 * @author xiewenbing
 * @date 2021/07/22
 */
@RestController
@RequestMapping("/tmall/jst/api")
@Slf4j
public class TmallQingeController {

    @Autowired
    private TmallService tmallService;

    @PostMapping("/createOrder")
    @ResponseBody
    public ResultDTO createOrder(HttpServletRequest httpServletRequest) {

        String content = RequestUtil.getPostParameter(httpServletRequest);
        if (StringUtil.isBlank(content)) {
            return ResultDTO.fail("数据为空");
        }

        log.info("【{}】接收到聚石塔渠道应用创建订单的请求，数据【{}】", TmallConsts.CHANNEL_NAME, content);

        Long orderId = 0L;
        try {
            TmallOrderCreateData orderData = JSON.parseObject(content, TmallOrderCreateData.class);
            orderId = tmallService.createOrder(orderData);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success(orderId,"OK");
    }

    @PostMapping("/changeDutytime")
    @ResponseBody
    public ResultDTO changeDutytime(HttpServletRequest httpServletRequest) {

        String content = RequestUtil.getPostParameter(httpServletRequest);
        if (StringUtil.isBlank(content)) {
            return ResultDTO.fail("数据为空");
        }

        log.info("【{}】接收到聚石塔渠道应用订单改约的请求，数据【{}】", TmallConsts.CHANNEL_NAME, content);

        try {
            TmallOrderReserveData orderData = JSON.parseObject(content, TmallOrderReserveData.class);
            tmallService.changeOrderDutytime(orderData);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success(null,"OK");
    }

    @PostMapping("/cancelOrder")
    @ResponseBody
    public ResultDTO cancelOrder(HttpServletRequest httpServletRequest) {

        String content = RequestUtil.getPostParameter(httpServletRequest);
        if (StringUtil.isBlank(content)) {
            return ResultDTO.fail("数据为空");
        }

        log.info("【{}】接收到聚石塔渠道应用取消订单的请求，数据【{}】", TmallConsts.CHANNEL_NAME, content);

        try {
            TmallOrderDataBase orderData = JSON.parseObject(content, TmallOrderReserveData.class);
            tmallService.cancelOrder(orderData);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success(null,"OK");
    }

    @PostMapping("/completeOrder")
    @ResponseBody
    public ResultDTO completeOrder(HttpServletRequest httpServletRequest) {

        String content = RequestUtil.getPostParameter(httpServletRequest);
        if (StringUtil.isBlank(content)) {
            return ResultDTO.fail("数据为空");
        }

        log.info("【{}】接收到聚石塔渠道应用订单核销完成的请求，数据【{}】", TmallConsts.CHANNEL_NAME, content);

        try {
            TmallOrderDataBase orderData = JSON.parseObject(content, TmallOrderReserveData.class);
            tmallService.completeOrder(orderData);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }

        return ResultDTO.success(null,"OK");
    }

}
